// pages/courseDetail/courseDetail.js
import {courseSignIn} from "../../services/courseReq";

const {
    getCourseDetail,
    courseGetSchedulesByIdAndStatus
} = require('../../services/courseReq')
let formatDate = (res) => {
    let time = new Date(res)
    return `${time.getFullYear()}-${time.getMonth()}-${time.getDate()}`
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        obj: {},
        userId: null,
        familyInfo: [],
        MemberId: null,
        show: false,
        targetActivityId: null,
        reason: "",
        Reasonshow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let familyInfo = this.data.familyInfo
        familyInfo = wx.getStorageSync("familyInfo").member;
        wx.showLoading({
            title: '加载中',
        })

        const id = options.id;

        let params = {
            courseId: id,
            pageNum: 1,
            pageSize: 999,
            status: 1
        }
        let obj = this.data.obj;
        courseGetSchedulesByIdAndStatus(params, res => {
            console.log(res)
            obj = res.data.data.list;

            try {
                obj.forEach(each => {
                        each["teachingscheduleDate"] = formatDate(each["teachingscheduleDate"]);
                        each["absentStatus"] = each?.courseabsentEntities[0]?.absentStatus;

                        each["reason"] = each["courseabsentEntities"]?.pop()?.reason;
                    }
                )
            } catch (e) {
                console.log(`e-->`, e)
            }
            console.log(`obj-->`, obj)
            this.setData({
                obj,
                familyInfo
            })
            wx.hideLoading()
        })
        let volunteerId = wx.getStorageSync("volunteerInfo").volunteerId;
        this.setData({
            userId: volunteerId
        })

    },
    handleToSignInShow() {
        this.setData({
            show: true
        })

    },
    onClose() {
        this.setData({
            show: false
        })
    },
    handleGoSign() {

        let volunteerId = this.data.MemberId;
        let targetActivityId = this.data.targetActivityId;

        wx.showLoading({
            title: "正在加载中"
        })
        let self = this;
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                wx.hideLoading()
                wx.showLoading({
                    title: "正在签到中..."
                })
                targetActivityId = res.result.split('/').pop()
                // console.log(targetActivityId, volunteerId)
                if (!volunteerId) return wx.showToast({
                    title: "请先选择签到用户",
                    icon: "error"
                });
                courseSignIn({targetActivityId, volunteerId}, res => {
                    // console.log(res)
                    wx.hideLoading()
                    if (res.data.code !== 0) return wx.showToast({
                        title: res.data.msg,
                        icon: "error"
                    })
                    return wx.showToast({
                        title: "签到成功",
                        icon: "success"
                    })
                })
            },
            fail: (res) => {
                wx.showToast({
                    title: "扫描失败",
                    icon: "error",
                    duration: 2000,
                });
            },
            complete: (res) => {
            },
        });
    },
    handleCourseLeave(event) {
        let id = event.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../courseLeave/courseLeave?id=" + id
        })
    },
    handleGoMap(e) {
        // console.log(e)
        let position = e.currentTarget.dataset.position
        wx.openLocation({
            latitude: position.lat,	//维度
            longitude: position.lng, //经度
            name: "目的地",	//目的地定位名称
            scale: 15,	//缩放比例
            address: position.address	//导航详细地址
        })
    },
    handleSelectMember(event) {
        console.log(event)
        let MemberId = this.data.MemberId;
        MemberId = event.currentTarget.dataset.id;
        // console.log(MemberId)
        this.setData({
            MemberId
        })

    },

    handleGetNotLeaveReason(e) {
        const {reason} = e.currentTarget.dataset;
        this.setData({
            reason,
            Reasonshow: true
        })


    }


})
